package com.ruoyi.match.config;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.util.Map;
@Component
public class MyMapConvConvertStrategy implements Converter<Map> {


    @Override
    public Class supportJavaTypeKey() {
        return Map.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Map convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(stringValue, Map.class);
    }

    @Override
    public CellData convertToExcelData(Map map, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(map);

        return new CellData(jsonString);
    }
}